package com.twu.biblioteca.behavior;

import com.twu.biblioteca.UserCenter;

import java.util.Scanner;

public class LoginBehavior implements Behavior {

    public static final String LOGIN_SUCCESS = "Successfully login!";
    public static final String LOGIN_FAILED = "Invalid username or password!!";

    @Override
    public String getMessage() {
        return null;
    }

    @Override
    public Behavior nextBehavior() {
        return new ListMenuOptionBehavior();
    }

    @Override
    public void execute() {
        UserCenter userCenter = UserCenter.getInstance();

        String username = getInput("username:");
        String password = getInput("password:");

        if (userCenter.login(username, password)) {
            System.out.println(LOGIN_SUCCESS);
        } else {
            System.out.println(LOGIN_FAILED);
        }
    }

    private String getInput(String label) {
        System.out.print(label);

        Scanner scanner = new Scanner(System.in);
        String input = "";
        while (input.equals("")) {
            input = scanner.nextLine().trim();
        }

        return input;
    }
}
